package dj.common.lang.pojo;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.dj.common.DeJiSerializable;
import io.swagger.annotations.ApiModel;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.Date;

/**
 * <br>
 * <p>
 * - 周期时段
 * </p>
 *
 * <br>
 *
 * @author ZJG
 * @version 1.0
 * @date 2021/11/19 上午9:10
 */
@Setter
@Getter
@Builder
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "Period", description = "周期;时段")
public class Period extends DeJiSerializable {
    private static final long serialVersionUID = 6775416478205471169L;

    /**
     * 开始时间
     */
    private Date startTime;

    /**
     * 结束时间
     */
    private Date endTime;

    /**
     * 开始-时间戳
     */
    private Long startTimestamp;

    /**
     * 结束-时间戳
     */
    private Long endTimestamp;

    /**
     * 开始时间
     * format-"yyyy-MM-dd HH:mm:ss"
     */
    private String startTimeStr;
    /**
     * 结束时间
     * format-"yyyy-MM-dd HH:mm:ss"
     */
    private String endTimeStr;


    public Date getStartTime() {
        if (ObjectUtil.isNull(startTime)) {
            if (ObjectUtil.isNotNull(getStartTimestamp())) {
                startTime = DateUtil.date(getStartTimestamp());
            } else if (StrUtil.isNotBlank(getStartTimeStr())) {
                startTime = DateUtil.parseTime(getStartTimeStr());
            }
        }
        return startTime;
    }

    public Date getEndTime() {
        if (ObjectUtil.isNull(endTime)) {
            if (ObjectUtil.isNotNull(getEndTimestamp())) {
                endTime = DateUtil.date(getEndTimestamp());
            } else if (StrUtil.isNotBlank(getEndTimeStr())) {
                endTime = DateUtil.parseTime(getEndTimeStr());
            }
        }
        return endTime;
    }

    public Long getStartTimestamp() {
        if (ObjectUtil.isNull(startTimestamp)) {
            if (ObjectUtil.isNotNull(getStartTime())) {
                startTimestamp = getStartTime().getTime();
            } else if (StrUtil.isNotBlank(getStartTimeStr())) {
                startTimestamp = DateUtil.parseTime(getStartTimeStr()).getTime();
            }
        }
        return startTimestamp;
    }

    public Long getEndTimestamp() {
        if (ObjectUtil.isNull(startTimestamp)) {
            if (ObjectUtil.isNotNull(getEndTime())) {
                endTimestamp = getEndTime().getTime();
            } else if (StrUtil.isNotBlank(getEndTimeStr())) {
                endTimestamp = DateUtil.parseTime(getEndTimeStr()).getTime();
            }
        }
        return endTimestamp;
    }

    public String getStartTimeStr() {
        if (ObjectUtil.isNull(startTimeStr)) {
            if (ObjectUtil.isNotNull(getStartTime())) {
                startTimeStr = DateUtil.format(getStartTime(), DatePattern.NORM_DATETIME_PATTERN);
            } else if (ObjectUtil.isNotNull(getStartTimestamp())) {
                startTimeStr =  DateUtil.format(DateUtil.date(getStartTimestamp()), DatePattern.NORM_DATETIME_PATTERN);
            }
        }
        return startTimeStr;
    }

    public String getEndTimeStr() {
        if (ObjectUtil.isNull(endTimeStr)) {
            if (ObjectUtil.isNotNull(getEndTime())) {
                endTimeStr = DateUtil.format(getEndTime(), DatePattern.NORM_DATETIME_PATTERN);
            } else if (ObjectUtil.isNotNull(getEndTimestamp())) {
                endTimeStr =  DateUtil.format(DateUtil.date(getEndTimestamp()), DatePattern.NORM_DATETIME_PATTERN);
            }
        }
        return endTimeStr;
    }
}
